package com.service.bean.oil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.service.bean.device.Vehicle;
import com.service.bean.signal.GpsSignal;
import com.service.cache.CacheManager;

public class OilBean {

	private Date changeTimeForm = null;
	private Date changeTimeTo = null;
	private double lastRealOilCal = 0.00;	
	private Date lastTime = null;
	private double rapidFlag = 10.00;	
	private double oilchangeTotal = 0.00;
	private Double changeOilFrom = null;
	private Double changeOilTo = null;
	private boolean isKeepAdd = false;
	
	/**
	 * 设置油量
	 * @param gpsTime
	 * @param realOilCal
	 * @throws ParseException
	 */
	public void setRealOilCal(GpsSignal signal,Vehicle vehicle) throws ParseException{
		if(lastRealOilCal != 0.0){
			double oilchange = new BigDecimal(Math.abs(signal.realOilCal-lastRealOilCal)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			this.isKeepAdd = false;
			//是否大于突变值
			if(oilchange>=rapidFlag){
				if(signal.realOilCal>lastRealOilCal){
					this.changeTimeForm = (changeTimeForm == null)?lastTime:this.changeTimeForm;
					//加油开始点：当前的上一条数据
					this.changeOilFrom = (changeOilFrom == null)?this.lastRealOilCal:this.changeOilFrom;						
					this.changeTimeTo = signal.gpsTime;	
					this.changeOilTo = signal.realOilCal;
					this.oilchangeTotal += oilchange;
					this.isKeepAdd = true;
				}
			}
			
			if(this.oilchangeTotal>0 && !this.isKeepAdd){
				OilChangeBean oilChangeBean = initOilChangeBean(signal,vehicle);
				CacheManager.instance().getOilChangeBeanSaveQueue(vehicle.company).offer(oilChangeBean);
				clear();
			}
		}

		//更新油耗
		setLastRealOilCal(signal);
	}

	
	public OilChangeBean initOilChangeBean(GpsSignal signal,Vehicle vehicle){
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(
				"yyyy-MM-dd");
		
		OilChangeBean oilChangeBean = new OilChangeBean();
		oilChangeBean.changeType = "1";
		oilChangeBean.companyCode = signal.companyCode;
		oilChangeBean.changeDate = simpleDateFormat2.format(signal.gpsTime);
		oilChangeBean.changeTimeForm = this.changeTimeForm;
		oilChangeBean.changeTimeTo = this.changeTimeTo;
		oilChangeBean.changeOilFrom = this.changeOilFrom;
		oilChangeBean.changeOilTo = this.changeOilTo;
		
		oilChangeBean.equipmentId = signal.equipmentId;
		oilChangeBean.changeAmt = oilchangeTotal;
		oilChangeBean.oilAmt = this.changeOilTo;
		
		if (vehicle.fuelPrice > 0) {
			oilChangeBean.payMode = "Card";
		} else {
			oilChangeBean.payMode = "Cash";
		}
		oilChangeBean.remark = simpleDateFormat.format(oilChangeBean.changeTimeTo)+"  加油["+oilchangeTotal+"]升！";
		oilChangeBean.status = "Active";
		
		if (vehicle.fuelPrice > 0) {
			oilChangeBean.oilUnitPrice = vehicle.fuelPrice;
			oilChangeBean.oilPrice = vehicle.fuelPrice*oilchangeTotal;
		}
		
		if (vehicle.oilCard != null){
			oilChangeBean.oilCardNO = vehicle.oilCard.oilCardNo;
			oilChangeBean.projectId = vehicle.oilCard.projectId;	
			if (vehicle.oilCard.oilCardId != null) {
				oilChangeBean.oilCardAmt = vehicle.oilCard.oilLeftAmt - vehicle.fuelPrice*oilchangeTotal;
				// 更新油卡中剩余油量
				//DbManager.instance().dbo.updateOilCard(vehicle.oilCard.oilCardId,oilChangeBean.oilCardAmt,vehicle.company);
				vehicle.oilCard.oilLeftAmt = oilChangeBean.oilCardAmt;
				CacheManager.instance().getUpdateOilCardQueue(vehicle.company).offer(vehicle.oilCard);
			}
		}
		
		return oilChangeBean;
	}
	
	
	private void clear(){
		this.changeTimeForm = null;
		this.changeOilFrom = null;
		this.oilchangeTotal = 0.00;	
	}
	
	public void setLastRealOilCal(GpsSignal signal){
		this.lastRealOilCal = signal.realOilCal;
		this.lastTime = signal.gpsTime;
	}
}
